<?php

namespace App\Repository\Commit;

use App\Models\Role as model;
use App\Repository\BaseRepository;

class RoleRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model;
    parent::__construct();
  }
  public function index($where, $page = 1, $limit = 10)
  {
    $cron = $where;
    $cron['order'] = ['orderby' => 'role_id', 'orderdir' => 'desc'];
    // $cron['where'] = $where;
    // var_dump($cron,111111111);
    $data = $this->search($this->model, $cron, $page, $limit);
    if (!empty($data['data'])) {
      $data['data'] = usfirstArr($data['data']);
    }
    return $data;
  }
  //添加
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'role_name',
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'role_name' => '权限名称不能为空',
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    //名称不能重复
    if (!empty($insertData['role_name'])) {
      $where = array(
        'role_name' => $insertData['role_name'],
        'shop_id' => $data->user->shop_id,
      );
      $isTrue = $this->model->where($where)->first();
      if ($isTrue) {
        return $this->returnMsg(
          3,
          '该名称已存在'
        );
      }
    }
    $insertData['create_time'] = date('Y-m-d H:i:s');
    $insertData['shop_id'] = !empty($data->user->shop_id) ? $data->user->shop_id : (!empty($data->user->id) ? $data->user->id : 1);
    $res = $this->model->insert($insertData);
    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }

  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    //用户姓名不能修改
    if (isset($field['role_name'])) {
      $whereIs = [
        'role_name' => $field['role_name'],
        'shop_id' => $data->user->shop_id
      ];
      $isTrue = $this->model->where($whereIs)->get();
      if (!empty($isTrue) &&  count($isTrue) > 1) {
        return $this->returnMsg(
          5,
          '该权限名称已存在'
        );
      }
    }
    //先查询是否存在
    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //查看  存入缓存
  public function sel($where = [], $page = 1, $limit = 10, $user = array())
  {
    //过滤查询条件
    //权限限制
    if ($user) {
      $where['liker'] = [
        'dept_str' => $user['dept_str'] . '%'
      ];
    }

    $res = $this->search($this->model, $where, $page, $limit);
    return $res;
  }
  //查看单条数据
  public function getOne($where)
  {
    return $this->model->where($where)->first();
  }
  //查询所有的数据 
  public function getAllData($where = [])
  {
    if (empty($where)) {
      $where[] = ['status', '=', 0];
    }
    $data =  $this->model->where($where)->get();
    $return = [];
    if (!empty($data)) {
      foreach ($data as $key => $v) {
        $return[] = usfirstArr($v);
      }
    }
    return $return;
  }
  public function getAllDataUser($where = [], $whereIn = [])
  {
    if (empty($where)) {
      $where[] = ['status', '=', 0];
    }
    $data =  $this->model->where($where)->whereIn('role_id', $whereIn)->get();
    $return = [];
    if (!empty($data)) {
      foreach ($data as $key => $v) {
        $return[] = usfirstArr($v);
      }
    }
    return $return;
  }
}
